layui.config({
	base : "/static/backend/js/"
}).use(['form','layer','jquery','table','laydate', 'layedit'],function(){
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
        table = layui.table,
        upload = layui.upload,
        layedit = layui.layedit,
		$ = layui.jquery;
    //初始化表格
    table.render({
        elem: '#list'
        , height: '700'
        , url: '/backend/config/notice_lists' //数据接口
        ,toolbar: '#toolbar'
        ,defaultToolbar:[]
        , page: true //开启分页
        ,limit : 40
        , cols: [[ //表头
            {field: 'id', title: 'ID',}
            , {field: 'title', title: '标题',}
            , {field: 'read', title: '导读',}
            , {field: 'create_time', title: '日期', sort: true,}
            ,{title: '操作', toolbar: '#barDemo', width: '16%',}

        ]]
    });
    //创建一个编辑器
    var editIndex = layedit.build('news_content', {
        uploadImage: {
            url: '/backend/config/uploadImg/'
            ,type: 'post'
        }
    });
    //添加
    $(".add").click(function(){
        layui.layer.open({
            title : "添加公告",
            type : 2,
            area: ['75%', '75%'],
            offset: ['', '18%'],
            content : "/backend/config/addNotice",
            success : function(layero, index){
            }
        })
    })
    //监听工具条
    table.on('tool(notice)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if(layEvent === 'edit'){ //GMOB与GMOD
            layui.layer.open({
                title : "编辑轮播图",
                type : 2,
                area: ['75%', '75%'],
                offset: ['', '18%'],
                content : "/backend/config/editNotice?id="+data.id,
                success : function(layero, index){
                }
            })

        } else if(layEvent === 'delete'){ //删除
            layer.confirm('真的删除行么', function(index){
                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                layer.close(index);
                //向服务端发送删除指令
                $.post("/backend/config/deleteNotice", {
                        id: data.id
                    }, function (data) {
                        if (data) {
                            top.layer.msg("删除成功");
                            window.location.reload();
                        } else {
                            top.layer.msg("出错了！！！");
                        }

                    }
                );
            });
        }
    });
    form.on("submit(addNews)", function (data) {
        data.field.content = layedit.getContent(editIndex);
        var formdom = $('#articleForm')[0]
            ,formdata = new FormData(formdom);
        formdata.append('content', layedit.getContent(editIndex));
        $.ajax({
            url: '/backend/config/addNotice',
            type: 'POST',
            data: formdata,
            processData: false, // jQuery不要去处理发送的数据
            contentType: false, // jQuery不要去设置Content-Type请求头
            success: function(res) {
                if(res.code==1){
                    top.layer.msg("公告添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }else{
                    layer.msg(res.msg);
                }

            }

        });


        return false;
    })
    form.on("submit(editNews)", function () {
        var formdom = $('#articleForm')[0]
            ,formdata = new FormData(formdom);
        formdata.append('content', layedit.getContent(editIndex));
        $.ajax({
            url: '/backend/config/editNotice',
            type: 'POST',
            data: formdata,
            processData: false, // jQuery不要去处理发送的数据
            contentType: false, // jQuery不要去设置Content-Type请求头
            success: function(res) {
                if(res.code==1){
                    top.layer.msg("公告修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }else{
                    layer.msg(res.msg);
                }

            }

        });

        return false;
    })
})
